package com.yb0os1.controller.admin;

import com.yb0os1.dto.DishDTO;
import com.yb0os1.entity.Dish;
import com.yb0os1.result.PageResult;
import com.yb0os1.result.Result;
import com.yb0os1.serve.DishServe;
import com.yb0os1.vo.DishVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

@Slf4j
@RestController("adminDishController")
@Tag(name = "处理菜品的各种操作")
@RequestMapping("/admin/dish")
public class DishController {
    @Autowired
    private DishServe dishServe;

    @Autowired
    private RedisTemplate redisTemplate;

    @Operation(summary = "新增菜品")
    @PostMapping
    public Result<String> addDish(@RequestBody DishDTO dishDTO){
        dishServe.addDishAndFlavor(dishDTO);
        //清理缓存数据
        String key = "dish_"+dishDTO.getCategoryId();
        clearCache(key);
        return Result.success();
    }

    @Operation(summary = "菜品分页查询")
    @GetMapping("/page")
    public Result<PageResult> pageSelect(Long categoryId,String name,Integer page,Integer pageSize,Integer status){
        PageResult pageResult = dishServe.pageSelect(categoryId, name, page, pageSize, status);
        log.info("分页查询的结果{}",pageResult);
        return Result.success(pageResult);
    }

    @Operation(summary = "批量删除菜品")
    @DeleteMapping
    public Result<String> deleteDish(@RequestParam List<Long> ids){
        dishServe.deleteDish(ids);
        // 可以删除所有的缓存  删除以dish_开头的key
        clearCache("dish_*");
        return Result.success();
    }
    @Operation(summary = "修改菜品")
    @PutMapping
    public Result<String> updateDish(@RequestBody DishDTO dishDTO){
        // 可以删除所有的缓存  删除以dish_开头的key  修改涉及两个表（一个加 一个山）
        clearCache("dish_*");
        dishServe.updateDish(dishDTO);
        return Result.success();
    }

    @Operation(summary = "根据ID查询菜品")
    @GetMapping("/{id}")
    public Result<DishVO> getDishById(@PathVariable Long id){
        return Result.success(dishServe.getDishById(id));
    }

    @GetMapping("/list")
    @Operation(summary = "根据分类id查询菜品")
    public Result<List<Dish>> getDishByTypeId(Long categoryId){
        return Result.success(dishServe.getDishByTypeId(categoryId));
    }

    @PostMapping("/status/{status}")
    @Operation(summary = "菜品起售、停售")
    public Result<String> isOpenCloseDish(@PathVariable Integer status,Long id){
        dishServe.updateStatus(status,id);
        clearCache("dish_*");
        return Result.success();
    }

    private void clearCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}

